﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.MetaAttribute;
using Csla.Wpf;
using OpenExpressApp.Command;
using Csla.Core;
using System.ComponentModel;
using System.Windows;
using OpenExpressApp.Template;
using OpenExpressApp.Types;
using Csla;
using OpenExpressApp.WPF.Controls;
using System.Collections.ObjectModel;
using log4net;
using System.Collections;
using OpenExpressApp.MetaModel;
using Avalon.Windows.Controls;
using OpenExpressApp.WPF.Templete;
using OpenExpressApp.Module;
using OpenExpressApp.Module.WPF;
using System.Windows.Controls;
using Ricciolo.Controls;
using System.Reflection;
using System.Windows.Markup;
using System.Diagnostics;
using AvalonDock;
using OpenExpressApp.Module.WPF.Editors;
using Csla.OpenExpressApp;

namespace OpenExpressApp.WPF.Command
{
    /// <summary>
    /// 保存一条记录
    /// </summary>
    [Command(CommandNames.Save_Bill, ImageName = "Save.bmp", Label = "保存", ToolbarType = ToolbarType.Main, ModuleType = ModuleType.Bill, ToolTip = "保存记录")]
    public class SaveBillCommand : WPFViewCommand
    {
        public override bool CanExecute(ObjectView view)
        {
            return (null != view.CurrentObject)
                && ((view.CurrentObject as ITrackStatus).IsDirty)
                //&& ((view.CurrentObject as ITrackStatus).IsValid)//暂时不加这条了。
                ;
        }
        public override void Execute(ObjectView view)
        {
            var currentObject = view.CurrentObject as BusinessBase;

            //检测条件
            var denpendentObject = currentObject as IDenpendentObject;
            if (denpendentObject != null)
            {
                denpendentObject.CheckRules();
                if (denpendentObject.BrokenRulesCollection.Count > 0)
                {
                    MessageBox.Show(denpendentObject.BrokenRulesCollection[0].Description, "保存出错");
                    return;
                }
            }

            //currentObject.ApplyEdit();

            DiffSaveCommand.Execute(currentObject);

            //currentObject.BeginEdit();
        }
    }
}
